package com.newtouch.bxzs.enterprise.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.newtouch.bxzs.common.base.BaseErrorEnum;
import com.newtouch.bxzs.common.base.BaseException;
import com.newtouch.bxzs.common.base.em.SystemParamsEnum;
import com.newtouch.bxzs.common.base.model.CustomerFamilyRelationship;
import com.newtouch.bxzs.common.base.util.CheckCertificateTypeUtil;
import com.newtouch.bxzs.common.base.util.RedisUtils;
import com.newtouch.bxzs.common.base.util.SendHttpRequestUtils;
import com.newtouch.bxzs.common.base.util.UserDetailUtil;
import com.newtouch.bxzs.enterprise.mapper.CustomerFamilyRelationshipMapper;
import com.newtouch.bxzs.enterprise.model.bo.CustomerFamilyRelationshipBO;
import com.newtouch.bxzs.enterprise.model.bo.FamilyInformationBO;
import com.newtouch.bxzs.enterprise.model.vo.CustomerFamilyRelationshipVO;
import com.newtouch.bxzs.enterprise.model.vo.CustomerFamilyVO;
import com.newtouch.bxzs.enterprise.model.vo.CustomerInformationVO;
import com.newtouch.bxzs.enterprise.service.CustomerFamilyRelationshipService;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Validator;

/**
 * 客户家庭关系service实现类
 *
 * <p>
 * <b>History:</b>
 * <table border="1">
 * <tr>
 * <th>Date</th>
 * <th>Operator</th>
 * <th>Memo</th>
 * </tr>
 * <tr>
 * <td>2021年3月19日</td>
 * <td>chenfugui</td>
 * <td>Create</td>
 * </tr>
 * </table>
 * 
 * @author chenfugui
 * 
 * @version 1.0.0
 * @since 1.0.0
 */
@Service
public class CustomerFamilyRelationshipServiceImpl implements CustomerFamilyRelationshipService {

	@Autowired
	private CustomerFamilyRelationshipMapper customerFamilyRelationshipMapper;

	@Autowired
	private RedisUtils redisUtils;

	/**
	 * 静态变量：系统日志
	 */
	private static final Log logger = LogFactory.getLog(CustomerFamilyRelationshipServiceImpl.class);


	@Override
	public List<CustomerFamilyRelationshipBO> getCustomerFamilyRelationshipList(
			CustomerFamilyRelationshipVO familyRelationshipVO) {
		List<CustomerFamilyRelationshipBO> allList = new ArrayList<CustomerFamilyRelationshipBO>();
		List<CustomerFamilyRelationshipBO> list2 = null;
		List<FamilyInformationBO> familyInformationBO = null;
		List<CustomerFamilyRelationshipBO> list3 = null;
		String userId = UserDetailUtil.getUserDetail().getUserId();
		List<CustomerFamilyRelationshipBO> list1 = customerFamilyRelationshipMapper
				.getFamliyList(familyRelationshipVO.getCustomerId(), userId);
		String status = redisUtils.get(SystemParamsEnum.BANKING_OR_INSURANCE.getCode());
		CustomerInformationVO customerInformationVO = new CustomerInformationVO();
		BeanUtil.copyProperties(familyRelationshipVO, customerInformationVO);
		// 调模拟端
		SendHttpRequestUtils<CustomerInformationVO> sendHttpRequestUtils = new SendHttpRequestUtils<>();
		familyInformationBO = JSONObject
				.parseArray(sendHttpRequestUtils
						.sendHttpRequestToArray(customerInformationVO,
								redisUtils.get(SystemParamsEnum.BANKING_OR_INSURANCE_INTERIOR_INFO.getCode()))
						.toJSONString(), FamilyInformationBO.class);
		logger.info("客户家庭普列表模拟端查询成功");
		list2 = assignment(familyInformationBO, status);
	
		// 调用对端助手
		SendHttpRequestUtils<CustomerFamilyRelationshipVO> sendHttpRequestUtils2 = new SendHttpRequestUtils<>();
		list3 = JSONObject
				.parseArray(
						sendHttpRequestUtils2
								.sendHttpRequestToArray(familyRelationshipVO,
										redisUtils
												.get(SystemParamsEnum.BANKING_OR_INSURANCE_OPPOSITE_END_INFO.getCode()))
								.toJSONString(),
						CustomerFamilyRelationshipBO.class);
		logger.info("客户家庭普列表对端查询成功");
		allList.addAll(list1);
		allList.addAll(list2);
		allList.addAll(list3);
		logger.info("客户家庭普列表查询成功");
		return allList;
	}


	@Override
	public String modifyCustomerFamilyRelationship(CustomerFamilyVO customerFamilyVO) {
		/* personalCustomerVO是否为空 */
		String userId = UserDetailUtil.getUserDetail().getUserId();
		if (BeanUtil.isEmpty(customerFamilyVO)) {
			return null;
		}
		CustomerFamilyRelationship cfr = new CustomerFamilyRelationship();
		BeanUtil.copyProperties(customerFamilyVO, cfr);
		checkCustomer(customerFamilyVO);
		if (StringUtils.isEmpty(customerFamilyVO.getFamilyRelationshipId())) {
			cfr.setDataSource("3");
			cfr.setCreateTime(new Date());
			cfr.setCreateUserId(userId);
			cfr.setIsDelete("0");
			customerFamilyRelationshipMapper.insert(cfr);
			logger.info("成功增加客户家人信息");
		} else {
			/* 判断是否为自建关系,是自建关系才可修改 */
			if (("3").equals(customerFamilyVO.getDataSource())) {
				cfr.setUpdateTime(new Date());
				cfr.setUpdateUserId(userId);
				customerFamilyRelationshipMapper.updateById(cfr);
				logger.info("成功修改客户家人信息");
			} else {
				throw new BaseException(BaseErrorEnum.NO_SELF_BUILT_DATA);
			}
		}

		return null;
	}

	@Override
	public String deleteCustomerFamilyRelationship(String familyRelationshipId, String dataSource) {
		/* dataSource位3是自建数据 */
		String userId = UserDetailUtil.getUserDetail().getUserId();
		if (("3").equals(dataSource)) {
			/* 删除家庭关系表数据将IS_DELETE字段置为1 */
			customerFamilyRelationshipMapper.deleteFamliy(familyRelationshipId, userId, "1");
			return "删除成功";
		} else {
			throw new BaseException(BaseErrorEnum.NO_SELF_BUILT_DATA);
		}

	}

	/**
	 * 校验必填项,证件,手机号
	 *
	 * @param customerFamilyVO
	 *            客户家人
	 */
	private void checkCustomer(CustomerFamilyVO customerFamilyVO) {
		/* 校验必填项 */
		if (StringUtils.isEmpty(customerFamilyVO.getFamilyName())
				|| StringUtils.isEmpty(customerFamilyVO.getFamilyPhone())
				|| StringUtils.isEmpty(customerFamilyVO.getFamilyCertificateType())
				|| StringUtils.isEmpty(customerFamilyVO.getFamilyCertificateNumber())
				|| StringUtils.isEmpty(customerFamilyVO.getRelationshipType())) {
			throw new BaseException(BaseErrorEnum.REQUIRED_ITEM_NULL);
		}

		if (customerFamilyVO.getFamilyName().length() >= 30) {
			throw new BaseException("姓名长度大于30");
		}
		/* 校验证件 */
		CheckCertificateTypeUtil.checkCertificate(customerFamilyVO.getFamilyCertificateType(),
				customerFamilyVO.getFamilyCertificateNumber());
		/* 验证手机号 */
		if (!Validator.isMobile(customerFamilyVO.getFamilyPhone())) {
			throw new BaseException(BaseErrorEnum.BODY_NOT_MATCH);
		}
	}

	public List<CustomerFamilyRelationshipBO> assignment(List<FamilyInformationBO> familyInformationBO, String status) {
		List<CustomerFamilyRelationshipBO> customerFamilyList = new ArrayList<CustomerFamilyRelationshipBO>();
		if (CollectionUtil.isNotEmpty(familyInformationBO)) {
			for (FamilyInformationBO list : familyInformationBO) {
				CustomerFamilyRelationshipBO customerFamily = new CustomerFamilyRelationshipBO();
				BeanUtil.copyProperties(list, customerFamily);
				customerFamily.setDataSource(status);
				customerFamilyList.add(customerFamily);
			}
		}

		return customerFamilyList;

	}

	public void assignmentList(List<CustomerFamilyRelationshipBO> list, String status) {
		if (CollectionUtil.isNotEmpty(list)) {
			for (CustomerFamilyRelationshipBO customerFamilyRelationshipBO : list) {
				customerFamilyRelationshipBO.setDataSource(status);
			}
		}
	}

	@Override
	public List<CustomerFamilyRelationshipBO> acceptionCustomerFamilyRelationship(
			CustomerFamilyRelationshipVO familyRelationshipVO) {
		List<CustomerFamilyRelationshipBO> list = new ArrayList<CustomerFamilyRelationshipBO>();
		List<FamilyInformationBO> familyInformationBO = null;
		List<CustomerFamilyRelationshipBO> list2 = null;
		CustomerInformationVO customerInformationVO = new CustomerInformationVO();
		BeanUtil.copyProperties(familyRelationshipVO, customerInformationVO);
		String status = redisUtils.get(SystemParamsEnum.BANKING_OR_INSURANCE.getCode());
		// 调模拟端
		SendHttpRequestUtils<CustomerInformationVO> sendHttpRequestUtils = new SendHttpRequestUtils<>();
		familyInformationBO = JSONObject
				.parseArray(sendHttpRequestUtils
						.sendHttpRequestToArray(customerInformationVO,
								redisUtils.get(SystemParamsEnum.BANKING_OR_INSURANCE_INTERIOR_INFO.getCode()))
						.toJSONString(), FamilyInformationBO.class);
		list2 = assignment(familyInformationBO, status);
		list.addAll(list2);
		logger.info("客户家庭普列表对端的模拟端查询成功");
		return list;
	}

}
